﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;

namespace ajayumi.develop.win32.Transport.WCF.Services
{
    /// <summary>
    /// 传输文件集合类
    /// Author  : 黄乙冬
    /// Date    : 2011-08-29
    /// </summary>
    public class TransFileCollection : BindingList<TransFileItem>
    {
        private readonly object m_LockObj = null;

        public TransFileCollection()
            : base()
        {
            this.m_LockObj = new object();
        }

        public new void Add(TransFileItem item)
        {
            lock (this.m_LockObj)
            {
                TransFileItem transFileItem = this.FirstOrDefault<TransFileItem>(o => o.RelativePath.Equals(item.RelativePath));
                if (transFileItem == null)
                {
                    item.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    item.EndTime = string.Empty;
                    base.Add(item);
                }
                else
                {
                    transFileItem.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    transFileItem.EndTime = string.Empty;
                }
            }
        }

        public virtual void TransFileReady(string relativePath, string fileSize, int progressPercentage)
        {
            TransFileItem item = this.FindByRelativePath(relativePath);
            if (item != null)
            {
                lock (this.m_LockObj)
                {
                    item.Status = ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Waiting;
                    item.FileSize = fileSize;
                    item.ProgressPercentage = string.Format("{0}%", progressPercentage);
                }
            }
        }

        public virtual void TransFileProgressChanged(string relativePath, int progressPercentage)
        {
            TransFileItem item = this.FindByRelativePath(relativePath);
            if (item != null)
            {
                lock (this.m_LockObj)
                {
                    item.Status = ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Connecting;
                    item.ProgressPercentage = string.Format("{0}%", progressPercentage);
                }
            }
        }

        //public virtual void TransFileCompleted(string relativePath)
        //{
        //    TransFileItem item = this.FindByRelativePath(relativePath);
        //    if (item != null)
        //    {
        //        lock (this.m_LockObj)
        //        {
        //            item.Status = ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Completed;
        //            item.ProgressPercentage = "100%";
        //            item.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        //        }
        //    }
        //}

        public virtual void TransFileCompleted(TransCompletedEventArgs e)
        {
            TransFileItem item = this.FindByRelativePath(e.RelativePath);
            if (item != null)
            {
                lock (this.m_LockObj)
                {
                    if (e.Error != null)
                    { item.Status = ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Error; }
                    else
                    {
                        item.Status = ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Completed;
                        item.ProgressPercentage = "100%";
                        item.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
        }

        /// <summary>
        /// 根据相对路径查找集合中的项
        /// </summary>
        /// <param name="relativePath">相对路径</param>
        /// <returns></returns>
        protected TransFileItem FindByRelativePath(string relativePath)
        {
            TransFileItem transFileItem = null;
            lock (this.m_LockObj)
            {
                transFileItem = (from item in this
                                 where item.RelativePath.Equals(Path.Combine(relativePath))
                                 select item).FirstOrDefault<TransFileItem>();
            }
            return transFileItem;
        }

        /// <summary>
        /// 移除已完成项
        /// </summary>
        public void ClearCompleted()
        {
            List<TransFileItem> lst = new List<TransFileItem>();
            foreach (TransFileItem item in this)
            {
                if (item.Status == ajayumi.develop.win32.Transport.WCF.Services.TransStatus.Completed)
                {
                    lst.Add(item);
                }
            }

            lock (this.m_LockObj)
            {
                foreach (TransFileItem item in lst)
                {
                    this.Remove(item);
                }
            }

        }




    }
}
